package com.xyx.controller;

import com.xyx.aspect.ApiOperationLog;
import com.xyx.enums.ResponseCodeEnum;
import com.xyx.exception.BizException;
import com.xyx.model.User;
import com.xyx.utils.JsonUtil;
import com.xyx.utils.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.stream.Collectors;

/**
 * 描述：<测试接口>
 *
 * @author：xiayixiang
 * @date: 21:07
 */
@RestController
@Slf4j
@Api(tags = "测试模块TestController")
public class TestController {

    @PostMapping("/test")
    @ApiOperationLog(description = "测试接口")
    public Response test(@RequestBody @Validated User user, BindingResult bindingResult) {
        // 是否存在校验错误
        if (bindingResult.hasErrors()) {
            // 获取校验不通过字段的提示信息
            String errorMsg = bindingResult.getFieldErrors()
                    .stream()
                    .map(FieldError::getDefaultMessage)
                    .collect(Collectors.joining(", "));

            return Response.fail(errorMsg);
        }
        if (1 == 1) {
            // 手动抛异常，入参是前面定义好的异常码枚举，返参统一交给全局异常处理器搞定
//            throw new BizException(ResponseCodeEnum.PRODUCT_NOT_FOUND);
        }

        // 返参
        return Response.success("参数没有任何问题");
    }

    @PostMapping("/test2")
    @ApiOperationLog(description = "测试接口")
    @ApiOperation(value = "测试接口")
    public Response test2(@RequestBody @Validated User user) {
        // 打印入参
        log.info(JsonUtil.toJsonString(user));

        // 设置三种日期字段值
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateDate(LocalDate.now());
        user.setTime(LocalTime.now());

        return Response.success(user);
    }

    @PostMapping("/login")
    @ApiOperationLog(description = "登录接口")
    @ApiOperation(value = "登录接口")
    public Response login(@RequestBody @Validated User user) {
        // 打印入参
        log.info(JsonUtil.toJsonString(user));
        // 设置三种日期字段值
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateDate(LocalDate.now());
        user.setTime(LocalTime.now());
        return Response.success(user);
    }

    @PostMapping("/admin/test")
    @ApiOperationLog(description = "admin测试")
    @ApiOperation(value = "admin测试")
    public Response admintest() {
        return Response.success("操作成功！");
    }

    @PostMapping("/admin/update")
    @ApiOperationLog(description = "测试更新接口")
    @ApiOperation(value = "测试更新接口")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public Response testUpdate() {
        log.info("更新成功...");
        return Response.success();
    }

}
